package com.blog.service.service;

import com.blog.check.check.BlogTypeCheck;
import com.blog.check.check.CheckResult;
import com.blog.check.result.Result;
import com.blog.check.resultenum.BlogTypeChkRsEnum;
import com.blog.dao.domain.BlogTypeDO;
import com.blog.dao.query.BlogTypeQuery;
import com.blog.exception.ServiceException;
import com.blog.form.BlogTypeForm;
import com.blog.service.dto.BlogTypeDTO;
import com.blog.until.BeanCopierUtil;
import com.blog.until.PrimaryKeyUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 博客类型服务
 *
 * @author 泡面ing
 * @date 2021/11/22
 */
@Service
public class BlogTypeService extends BaseService {

    private BlogTypeCheck blogTypeCheck;

    @Autowired
    public void setBlogTypeCheck(BlogTypeCheck blogTypeCheck){
        this.blogTypeCheck = blogTypeCheck;
    }



    @Transactional(rollbackFor = Exception.class)
    public Result list(BlogTypeQuery blogTypeQuery,int pageNum,int pageSize) throws Exception{

        List<BlogTypeDTO> blogTypeDTOList = new ArrayList<>();
        List<BlogTypeDO> blogTypeDOList = blogTypeDAO.list(blogTypeQuery);

        if (blogTypeDOList.size() == 0){
            return Result.success(blogTypeDOList);
        }
        for (BlogTypeDO blogTypeDO : blogTypeDOList){
            BlogTypeDTO blogTypeDTO = getShowDTO(blogTypeDO);
            blogTypeDTOList.add(blogTypeDTO);
        }
        PageHelper.startPage(pageNum,pageSize);
        PageInfo<BlogTypeDTO> pageInfo = new PageInfo<>(blogTypeDTOList);
        return Result.success(pageInfo);
    }

    private BlogTypeDTO getShowDTO(BlogTypeDO blogTypeDO) {
        BlogTypeDTO blogTypeDTO = new BlogTypeDTO();
        blogTypeDTO.setId(blogTypeDO.getId());
        blogTypeDTO.setOrderNo(blogTypeDO.getOrderNo());
        blogTypeDTO.setTypeName(blogTypeDO.getTypeName());
        return blogTypeDTO;
    }

    @Transactional(rollbackFor = Exception.class)
    public Result findByName(BlogTypeQuery blogTypeQuery){
        try{
            List<BlogTypeDTO> blogTypeDTOList = new ArrayList<>();

            List<BlogTypeDO> blogTypeDOList = blogTypeDAO.findByName(blogTypeQuery.getTypeName());
            for (BlogTypeDO blogTypeDO : blogTypeDOList){
                BlogTypeDTO blogTypeDTO = getShowDTO(blogTypeDO);
                blogTypeDTOList.add(blogTypeDTO);
            }
            return Result.success(blogTypeDTOList);

        }catch (Exception e){
            throw new ServiceException(e);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public Result save(BlogTypeForm blogTypeForm){
        try{
            CheckResult checkResult = blogTypeCheck.checkJurId();
            if (checkResult.getCode() != BlogTypeChkRsEnum.SUCCESS.getCode()){
                logger.error(logInfo(checkResult));
                return Result.failure(checkResult);
            }
            BlogTypeDO blogTypeDO = buildSaveBlogType(blogTypeForm);
            blogTypeDAO.save(blogTypeDO);
            return Result.success();
        }catch (Exception e){
            throw new ServiceException(e);
        }
    }

    @SneakyThrows
    private BlogTypeDO buildSaveBlogType(BlogTypeForm blogTypeForm) {
        BlogTypeDO blogTypeDO = BeanCopierUtil.create(blogTypeForm,BlogTypeDO.class);
        String id = PrimaryKeyUtil.get();
        blogTypeDO.setId(id);
        blogTypeDO.setTypeName(blogTypeForm.getTypeName());
        return blogTypeDO;
    }
    @Transactional(rollbackFor = Exception.class)
    public Result update(String blogTypeId,BlogTypeForm blogTypeForm){
        try{
            CheckResult checkResult = blogTypeCheck.checkUpdate(blogTypeId);
            if (checkResult.getCode() != BlogTypeChkRsEnum.SUCCESS.getCode()){
                logger.error(logInfo(checkResult));
                return Result.failure(checkResult);
            }

            BlogTypeDO blogTypeDO = buildUpdateBlogType(blogTypeId,blogTypeForm);
            blogTypeDAO.update(blogTypeDO);
            return Result.success();
        }catch (Exception e){
            throw new ServiceException(e);
        }
    }
    @SneakyThrows
    private BlogTypeDO buildUpdateBlogType(String blogTypeId,BlogTypeForm blogTypeForm) {
        BlogTypeDO blogTypeDO = blogTypeDAO.get(blogTypeId);
        BlogTypeDO newBlogTypeDO = BeanCopierUtil.create(blogTypeDO,BlogTypeDO.class);

        if (StringUtils.isNotBlank(blogTypeForm.getTypeName())){
            newBlogTypeDO.setTypeName(blogTypeForm.getTypeName());
        }
        return newBlogTypeDO;
    }
    @Transactional(rollbackFor = Exception.class)
    public Result remove(String blogTypeId){
        try{
            blogTypeDAO.remove(blogTypeId);
            return Result.success();
        }catch (Exception e){
            throw new ServiceException(e);
        }
    }
}
